package my.project.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import my.project.admin.common.dto.SysMenuDto;
import my.project.admin.entity.SysMenu;
import my.project.admin.entity.SysRoleMenu;
import my.project.admin.entity.SysUser;
import my.project.admin.mapper.SysMenuMapper;
import my.project.admin.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import my.project.admin.service.ISysRoleMenuService;
import my.project.admin.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zc
 * @since 2021-05-12
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    ISysRoleMenuService sysRoleMenuService;

    //获取当前用户菜单
    @Override
    public List<SysMenuDto> getUserNav() {

        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser user = sysUserService.getByUsername(username);

        String subSql = "select role_id from sys_user_role where user_id=" + user.getId();
        List<SysRoleMenu> roleMenus = sysRoleMenuService.list(new QueryWrapper<SysRoleMenu>()
                .select("distinct menu_id").inSql("role_id", subSql));
        List<Long> menuIds = roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        List<SysMenu> menus = this.listByIds(menuIds);

        List<SysMenu> menuTree = buildMenuTree(menus);
        return convert(menuTree);
    }

    @Override
    public List<SysMenu> menuTree() {
        // 获取所有菜单信息
        List<SysMenu> sysMenus = this.list(new QueryWrapper<SysMenu>().orderByAsc("orderNum"));

        // 转成树状结构
        return buildMenuTree(sysMenus);
    }

    //构造菜单树
    private List<SysMenu> buildMenuTree(List<SysMenu> menus) {

        List<SysMenu> finalMenu = new ArrayList<>();

        for (SysMenu menu1: menus) {
            //寻找各自子节点
            for (SysMenu menu2: menus) {
                if (menu1.getId().equals(menu2.getParentId())) {
                    menu1.getChildren().add(menu2);
                }
            }
            //提取出父节点
            if (menu1.getParentId() == 0L) {
                finalMenu.add(menu1);
            }
        }

        return finalMenu;
    }

    //将菜单树转换成Dto
    private List<SysMenuDto> convert(List<SysMenu> menuTree) {

        List<SysMenuDto> menuDtos = new ArrayList<>();

        menuTree.forEach(m -> {
            SysMenuDto dto = new SysMenuDto();
            dto.setId(m.getId());
            dto.setName(m.getPerms());
            dto.setTitle(m.getName());
            dto.setComponent(m.getComponent());
            dto.setPath(m.getPath());

            if (m.getChildren().size() > 0) {
                dto.setChildren(convert(m.getChildren()));
            }

            menuDtos.add(dto);
        });

        return menuDtos;
    }
}
